package com.service.admin.order.controller;

import com.service.admin.member.service.MemberService;
import com.service.admin.member.service.MemberStoreService;
import com.service.admin.order.controller.req.OrderReq;
import com.service.admin.order.service.OrderProductHrComboService;
import com.service.admin.product.service.ProductHrComboService;
import com.service.admin.support.MemberStoreLockUtils;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.OneResponse;
import com.service.admin.framework.log.RequestLog;
import com.service.admin.order.controller.req.OrderHrComboPageReq;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.model.Member;
import com.service.dao.model.MemberStore;
import com.service.dao.model.ProductHrCombo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 算力订单
 *
 * @author xxxx
 * @email xxxx@qq.com
 * @date 2019-11-10 22:06:11
 */
@RestController
@RequestMapping("order-product-hr-combo")
public class OrderProductHrComboController extends BaseController {
    @Autowired
    private OrderProductHrComboService orderProductHrComboService;

    @Autowired
    private ProductHrComboService productHrComboService;

    @Autowired
    private MemberStoreService memberStoreService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberStoreLockUtils storeLockUtils;

    @RequestLog("产品订单接口")
    @ApiOperation("分页")
    @PostMapping("page")
    public OneResponse page(@ApiParam @RequestBody OrderHrComboPageReq pageReq) {
        return successQueryOneResponse(orderProductHrComboService.pageOrder(pageReq));
    }

    @ApiOperation("获取订单详情")
    @RequestLog("获取订单详情")
    @PostMapping("detail/{orderId}")
    public OneResponse orderDetail(@PathVariable("orderId") Integer orderId) {
        return successQueryOneResponse(orderProductHrComboService.getOrderDetail(orderId));
    }


    @ApiOperation("创建用户订单")
    @RequestLog("创建订单")
    @PostMapping("create")
    public BaseResponse create(@RequestBody @Validated OrderReq orderReq){
        ProductHrCombo combo = productHrComboService.getById(orderReq.getComboId());
        if (ObjectUtils.isEmpty(combo) || !ObjectUtils.nullSafeEquals(combo.getComboStatus(), CommonStatusEnums.ACTIVE.getCode())) {
            throw new CommonException("不存在此套餐", "hash_rate_combo_not_exist");
        }
        Member member = memberService.getMemberByAccount(orderReq.getUserAccount());
        if (ObjectUtils.isEmpty(member) || !ObjectUtils.nullSafeEquals(member.getUserStatus(), CommonStatusEnums.ACTIVE.getCode())) {
            throw new CommonException("会员信息不正确");
        }
        //付款USDT
        MemberStore memberStore = memberStoreService.getMemberStore(member.getId(), CoinsEnum.USDT.getName());
        if (storeLockUtils.lock(memberStore.getMemberId(), memberStore.getId())) {
            try {
                orderProductHrComboService.create(orderReq, combo, member, memberStore);
            } finally {
                storeLockUtils.unlock(memberStore.getMemberId(), memberStore.getId());
            }
        }
        return successBaseResponse();
    }

//    @ApiOperation("填充算力")
//    @RequestLog("填充算力")
//    @PostMapping("padding")
//    public BaseResponse padding(@RequestBody @Validated OrderReq orderReq){
//        ProductHrCombo combo = productHrComboService.getById(orderReq.getComboId());
//        if (ObjectUtils.isEmpty(combo) || !ObjectUtils.nullSafeEquals(combo.getComboStatus(), CommonStatusEnums.ACTIVE.getCode())) {
//            throw new CommonException("不存在此套餐", "hash_rate_combo_not_exist");
//        }
//        Member member = memberService.getMemberByAccount(orderReq.getUserAccount());
//        if (ObjectUtils.isEmpty(member) || !ObjectUtils.nullSafeEquals(member.getUserStatus(), CommonStatusEnums.ACTIVE.getCode())) {
//            throw new CommonException("会员信息不正确");
//        }
//        //付款USDT
//        MemberStore memberStore = memberStoreService.getMemberStore(member.getId(), CoinsEnum.USDT.getName());
//        if (storeLockUtils.lock(memberStore.getMemberId(), memberStore.getId())) {
//            try {
//                orderProductHrComboService.create(orderReq, combo, member, memberStore);
//            } finally {
//                storeLockUtils.unlock(memberStore.getMemberId(), memberStore.getId());
//            }
//        }
//        return successBaseResponse();
//    }
}
